import type { RequestClientConfig } from '@vben/request';

import { downloadRequestClient, requestClient } from '#/api/request';

export namespace FileApi {
  /** 文件接口参数 */
  export interface ShareFile {
    fromUser: string;
    toUsers: Array<string>;
    fileId: string;
  }
  export interface ImportFile {
    fileId: string;
    catalogId: string;
  }
  export interface CreateNewFolderParams {
    userId: string;
    parentCatalogId: string;
    catalogName: string;
  }
  export interface DeleteFileDTO {
    catalogId: string;
    fileId: string;
    userId: string;
  }
  export interface PublishData {
    name: string;
    srid?: string;
    code?: string;
    bbox?: Array<number>;
  }
  export interface PublishTMSData {
    userId: string;
    serviceName: string;
    tifFileId: string;
    tilesZipId: Array<number>;
  }
  export interface UnZipData {
    userId: string;
    // 所谓fileId其实就是目标文件夹的id
    fileId: string;
    catalogId: string;
  }
}

const createNewFolder = (params: FileApi.CreateNewFolderParams) => {
  return requestClient.post('/dsc-systemMS/dsc-catalog', params);
};

const getChildren = (
  catalogId: string,
  pageSize: number,
  currentPage: number,
  keyword: string,
) => {
  return requestClient.get(
    `/dsc-systemMS/dsc-catalog/getChildrenByPageable/${catalogId}/${pageSize}/${currentPage}?keyword=${keyword}`,
  );
};
const getChildrenofcatalog = (catalogId: string) => {
  return requestClient.get(
    `/dsc-systemMS/dsc-catalog/getChildren/${catalogId}`,
  );
};

const getFileInfo = (fileId: string) => {
  return requestClient.get(`/dsc-systemMS/dsc-file/getFileInfo/${fileId}`);
};

const deleteCatalog = (catalogId: string) => {
  return requestClient.delete(`/dsc-systemMS/dsc-catalog/delete/${catalogId}`);
};

const deleteFile = (deleteFileDTO: FileApi.DeleteFileDTO) => {
  return requestClient.delete('/dsc-systemMS/dsc-file', {
    data: deleteFileDTO,
  });
};

const downloadFile = (fileId: string) => {
  return downloadRequestClient.get(
    `/dsc-systemMS/dsc-file/download/${fileId}`,
    {
      responseType: 'blob', // 确保返回二进制数据
    } as RequestClientConfig,
  );
};
const getFilePreviewUrl = (fileId: string) => {
  return requestClient.get(
    `/dsc-systemMS/dsc-file/getFilePreviewUrl/${fileId}`,
  );
};

const publishShp2VectorService = (data: FileApi.PublishData) => {
  return requestClient.post(
    '/dsc-systemMS/dsc-vector-service/publishShp2VectorS',
    data,
  );
};

const publishGeojson2VectorService = (data: FileApi.PublishData) => {
  return requestClient.post(
    '/dsc-systemMS/dsc-vector-service/publishGeoJSON2VectorS',
    data,
  );
};

const publishImage2RasterService = (data: FileApi.PublishData) => {
  return requestClient.post(
    '/dsc-systemMS/dsc-raster-service/publishImage2RasterS',
    data,
  );
};
const publishTiff2TMS = (data: FileApi.PublishTMSData) => {
  return requestClient.post(
    '/dsc-systemMS/dsc-raster-service/publishTiff2TMS',
    data,
  );
};
const publishTiff2ImgRasterS = (data: any) => {
  return requestClient.post(
    '/dsc-systemMS/dsc-raster-service/publishTiff2ImgRasterS',
    data,
  );
};
const getFolderTree = (rootCatalog: string) => {
  return requestClient.get(
    `/dsc-systemMS/dsc-catalog/getOnlyCatalogTree/${rootCatalog}`,
  );
};

const unZip = (data: FileApi.UnZipData) => {
  return requestClient.post('/dsc-systemMS/dsc-file/unzip', data);
};

// upload
const uploadFile = (userId: string, taskId: string, catalogId: string) => {
  const uploadInfo = {
    userId,
    taskId,
    catalogId,
  };
  return requestClient.post(
    '/dsc-systemMS/dsc-file',
    JSON.stringify(uploadInfo),
  );
};

const shareFile = (shareFileObj: FileApi.ShareFile) => {
  return requestClient.post('/dsc-systemMS/dsc-file/share', shareFileObj);
};

const importFile = (importFileObj: FileApi.ImportFile) => {
  return requestClient.post(
    '/dsc-systemMS/dsc-file/importResource',
    importFileObj,
  );
};
const renameCatalog = (catalogId: string, newName?: string) => {
  return requestClient.post(
    `/dsc-systemMS/dsc-catalog/rename/${catalogId}/${newName}`,
  );
};
const renameFile = (fileId: string, newName?: string) => {
  return requestClient.post(
    `/dsc-systemMS/dsc-catalog/rename/${fileId}/${newName}`,
  );
};

export default {
  createNewFolder,
  getChildren,
  getChildrenofcatalog,
  getFileInfo,
  deleteCatalog,
  deleteFile,
  downloadFile,
  getFilePreviewUrl,
  publishShp2VectorService,
  publishGeojson2VectorService,
  publishImage2RasterService,
  publishTiff2ImgRasterS,
  getFolderTree,
  unZip,
  uploadFile,
  shareFile,
  importFile,
  publishTiff2TMS,
  renameCatalog,
  renameFile,
};
